<?php
class Inbox_model extends MY_Model
{
	public
		$table = 'users_message_inbox',
		$prKey = 'inbox_id',
		$userID = null
	;

	function __construct()
	{
		parent::__construct();

		$this->fields = array(
			'user_ID'				=> T_('Question'),
			'date_posted'			=> T_('Date Posted'),
			'status' 				=> T_('Status'),
			'sender'				=> T_('From'),
			'message_header'		=> T_('Message Header'),
			'message_text'			=> T_('Message Text'),
			'referral_no'			=> T_('Referral No'),
			'ask_price'				=> T_('Ask'),
			'bid_price'				=> T_('Bid'),
		);
			
		$this->rules = array(
			'user_ID'			=> "trim|required|integer",
			'date_posted'		=> "trim|required|max_length[255]",
			'status' 			=> "trim|required|is_natural_no_zero",
			'sender'			=> "trim|required|max_length[255]",
			'message_header'	=> "trim|required|max_length[255]",
			'message_text'		=> "trim|required|max_length[255]",
			'referral_no'		=> "trim|required|max_length[255]",
			'ask_price'			=> "trim|required|max_length[255]",
			'bid_price'			=> "trim|required|max_length[255]",
		);
	}
	
	function getMessageInbox() {
    	
    	$sql = "SELECT * 
    			FROM ".$this->table."
    			WHERE inbox_user_ID='".$this->userID."'
    			ORDER BY inbox_date_posted DESC";
    	
    	$result = $this->db->query($sql)->result_array();
    	
    	$this->data['message'] = $this->statusToString($result);
    	
    	$this->data['number'] = $this->countNewInbox();
		
    }
    
	function countNewInbox() {
		
		/*$sql = "SELECT COUNT(*) AS number
				FROM ".$this->table."
				WHERE inbox_read_status=2 AND inbox_user_ID=".$this->userID;
		
		$result = $this->db->query($sql)->row_array();
		$this->data['inbox_number'] = $result['number'];
		return $result['number'];
		*/
		return 1;	
	}
	function setReadStatus($id, $read_status) {
		if (!empty($id) && !empty($read_status)) {
			
			$data['inbox_read_status'] = $read_status;
			$data['inbox_id'] = $id;
			
      		$res = parent::update($data);
      		return TRUE;      
    	}   
	}
	function search($id = null){
		
		$sql = "SELECT * 
    			FROM ".$this->table."
    			WHERE inbox_id='".$id."'";
    	
    	$result = $this->db->query($sql)->row_array();
    	
		$status = $this->config->item('request_status');
    	$result['inbox_status'] = $status[$result['inbox_status']];
    	
    	$this->data['message'] = $result;
	}
	function delete($items_serialized = null) {
		
		
		$items = explode("&", $items_serialized);
		
		$report = "";
		foreach ($items as $id)
		{	

			$res = $this->db->delete($this->table, array($this->prKey => $id));				
			
			if (is_null($res))
			{
				$this->log->writeMessage('error', T_('Error. Inbox Template was not removed.'));
			}
			else
			{
				$this->log->writeMessage('done', T_('Inbox Template was removed.'));					
			}
		}
		
	}
	function insert($data) {
		if (!empty($data))
		{
			parent::insert($data);

			if ($this->db->insert_id() > 0)
			{
				$this->log->writeMessage('done', T_('Message was sent succesfully.'));

				return TRUE;
			}
			else
			{
				$this->log->writeMessage('error', T_('Error! Message was not sent.'));

				return FALSE;
			}
		}
	}
	function statusToString($result) {
		$status = $this->config->item('request_status');

    	foreach($result as $key => $value) {
    		$result[$key]['inbox_status'] = $status[$value['inbox_status']];
    	}
    	
    	return $result;
	}
}
?>